package br.com.exemplo.swing;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class ExemploJFileChooserWithImage {

	public static void main(String[] args) {
		
		final JFrame janela = new JFrame("Titulo da Janela");

		final JPanel painel = new JPanel(new BorderLayout());
		janela.add(painel);

		JButton botao = new JButton("Arquivos");
		painel.add(botao, BorderLayout.SOUTH);
		
		final JLabel labelImg = new JLabel();
		
		botao.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				JFileChooser fileChooser = new JFileChooser();
				
				int retorno = fileChooser.showOpenDialog(null);
				
				if (retorno == JFileChooser.APPROVE_OPTION) {
					File file = fileChooser.getSelectedFile();
					
					try {
						BufferedImage myPicture = ImageIO.read(file);
						labelImg.setIcon(new ImageIcon(myPicture));
					} catch (IOException e) {
						labelImg.setText("Arquivo de imagem invalido!");
					}
					
				} else {
					labelImg.setText("Usuario cancelou a operacao");
				}
				
				painel.add(labelImg, BorderLayout.CENTER);
				janela.pack();
				janela.setSize(500, 500);
				janela.setVisible(true);
			}
		});

		janela.setSize(500, 500);
		janela.setVisible(true);
		janela.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	}

}
